<?php

namespace Nece\Sdk\Coze;

use GuzzleHttp\Client as GuzzleHttpClient;
use Nece\Sdk\Coze\Api\Audio;
use Nece\Sdk\Coze\Api\Bot;
use Nece\Sdk\Coze\Api\Chat;
use Nece\Sdk\Coze\Api\Connectors;
use Nece\Sdk\Coze\Api\Conversation;
use Nece\Sdk\Coze\Api\Datasets;
use Nece\Sdk\Coze\Api\Files;
use Nece\Sdk\Coze\Api\Folders;
use Nece\Sdk\Coze\Api\Variables;
use Nece\Sdk\Coze\Api\Workflow;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;

class Client
{
    private $api_base_url = 'https://api.coze.cn';
    private $http_client;
    private $client_id;
    private $client_secret;
    private $private_key;

    private $access_token;
    private $logger;
    private $cacher;

    // 各模块实例存储
    private $audio_instance;
    private $bot_instance;
    private $chat_instance;
    private $connectors_instance;
    private $conversation_instance;
    private $datasets_instance;
    private $files_instance;
    private $folders_instance;
    private $variables_instance;
    private $workflow_instance;

    /**
     * 构造函数
     *
     * @param string $client_id 应用ID
     * @param string $client_secret 公钥指纹
     * @param string $private_key 私钥
     * @param array $http_config HTTP客户端配置
     */
    public function __construct(string $client_id, string $client_secret, string $private_key, array $http_config = [])
    {
        $http_config['base_uri'] = $this->api_base_url;
        $this->http_client = new GuzzleHttpClient($http_config);

        $this->client_id = $client_id;
        $this->client_secret = $client_secret;
        $this->private_key = $private_key;
    }

    /**
     * 设置日志记录器
     *
     * @param LoggerInterface $logger 日志记录器接口
     * @return void
     */
    public function setLogger(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * 设置缓存器
     *
     * @param CacheInterface $cache 缓存接口
     * @return void
     */
    public function setCacher(CacheInterface $cache)
    {
        $this->cacher = $cache;
    }

    /**
     * 获取访问令牌
     *
     * @return AccessToken
     */
    public function getAccessToken()
    {
        if (!$this->access_token) {
            $this->access_token = new AccessToken($this->client_id, $this->client_secret, $this->private_key, $this->http_client, $this->cacher);
        }
        return $this->access_token;
    }

    /**
     * 音频
     *
     * @author gjw
     * @created 2025-04-23 13:21:06
     *
     * @return Audio
     */
    public function audio()
    {
        if (!$this->audio_instance) {
            $this->audio_instance = new Audio($this->getAccessToken(), $this->http_client, $this->logger);
        }
        return $this->audio_instance;
    }

    /**
     * 空间与智能体
     *
     * @author gjw
     * @created 2025-04-23 13:21:31
     *
     * @return Bot
     */
    public function bot()
    {
        if (!$this->bot_instance) {
            $this->bot_instance = new Bot($this->getAccessToken(), $this->http_client, $this->logger);
        }
        return $this->bot_instance;
    }

    /**
     * 对话
     *
     * @author gjw
     * @created 2025-04-24 13:04:52
     *
     * @return Chat
     */
    public function chat()
    {
        if (!$this->chat_instance) {
            $this->chat_instance = new Chat($this->getAccessToken(), $this->http_client, $this->logger);
        }
        return $this->chat_instance;
    }

    /**
     * 渠道
     *
     * @author gjw
     * @created 2025-04-23 13:22:01
     *
     * @return Connectors
     */
    public function connectors()
    {
        if (!$this->connectors_instance) {
            $this->connectors_instance = new Connectors($this->getAccessToken(), $this->http_client, $this->logger);
        }
        return $this->connectors_instance;
    }

    /**
     * 对话
     *
     * @author gjw
     * @created 2025-04-23 10:30:46
     *
     * @return Conversation
     */
    public function conversation()
    {
        if (!$this->conversation_instance) {
            $this->conversation_instance = new Conversation($this->getAccessToken(), $this->http_client, $this->logger);
        }
        return $this->conversation_instance;
    }

    /**
     * 知识库
     *
     * @author gjw
     * @created 2025-04-23 13:22:09
     *
     * @return Datasets
     */
    public function datasets()
    {
        if (!$this->datasets_instance) {
            $this->datasets_instance = new Datasets($this->getAccessToken(), $this->http_client, $this->logger);
        }
        return $this->datasets_instance;
    }

    /**
     * 文件
     *
     * @author gjw
     * @created 2025-04-23 13:22:21
     *
     * @return Files
     */
    public function files()
    {
        if (!$this->files_instance) {
            $this->files_instance = new Files($this->getAccessToken(), $this->http_client, $this->logger);
        }
        return $this->files_instance;
    }

    public function folders()
    {
        if (!$this->folders_instance) {
            $this->folders_instance = new Folders($this->getAccessToken(), $this->http_client, $this->logger);
        }
        return $this->folders_instance;
    }

    /**
     * 变量
     *
     * @author gjw
     * @created 2025-04-23 13:22:29
     *
     * @return Variables
     */
    public function variables()
    {
        if (!$this->variables_instance) {
            $this->variables_instance = new Variables($this->getAccessToken(), $this->http_client, $this->logger);
        }
        return $this->variables_instance;
    }

    /**
     * 工作流
     *
     * @author gjw
     * @created 2025-04-23 13:22:38
     *
     * @return Workflow
     */
    public function workflow()
    {
        if (!$this->workflow_instance) {
            $this->workflow_instance = new Workflow($this->getAccessToken(), $this->http_client, $this->logger);
        }
        return $this->workflow_instance;
    }
}
